package api

import (
	"fmt"
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"getaway/untils"
	"github.com/gin-gonic/gin"
)

// GetLocationInfo
// @Summary      获取地址信息
// @Description  获取地址信息
// @Tags         地图模块
// @Accept       json
// @Produce      json
// @Param        map  query      request.GetLocReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /map/getLocInfo [get]
// @Security TokenAuth
func GetLocationInfo(c *gin.Context) {
	var req request.GetLocReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	loc, err := untils.MapInfo(req.Lat, req.Lnt)
	if err != nil {
		response.SrvError(c, "查询地址失败", err.Error())
		return
	}

	// 记录获取地址信息行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "获取地址信息", map[string]interface{}{
		"lat":     req.Lat,
		"lnt":     req.Lnt,
		"success": err == nil,
	})

	response.Success(c, "获取成功", loc)
}

// RouteRecommend
// @Summary      路线推荐
// @Description  路线推荐
// @Tags         地图模块
// @Accept       json
// @Produce      json
// @Param        map  query      request.RouteRecommendReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /map/routeRecommend [get]
// @Security TokenAuth
func RouteRecommend(c *gin.Context) {
	var req request.RouteRecommendReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	route, err := config.OrderClient.RouteRecommend(c, &__.RouteRecommendReq{
		StartAddress: req.StartAddress,
		EndAddress:   req.EndAddress,
		Tactics:      req.Tactics,
	})
	if err != nil {
		response.SrvError(c, "查询路线失败", err.Error())
		return
	}

	// 记录路线推荐行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "路线推荐查询", map[string]interface{}{
		"startAddress": req.StartAddress,
		"endAddress":   req.EndAddress,
		"tactics":      req.Tactics,
		"success":      err == nil,
	})

	response.Success(c, "获取成功", route)
}

// AddressLocation
// @Summary      地址查询经纬度
// @Description  地址查询经纬度
// @Tags         地图模块
// @Accept       json
// @Produce      json
// @Param        map  query      request.AddressLocationReq  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /map/addressLocation [get]
// @Security TokenAuth
func AddressLocation(c *gin.Context) {
	var req request.AddressLocationReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	fmt.Println(req.Address)
	loc, err := config.OrderClient.AddressLocation(c, &__.AddressLocationReq{
		Address: req.Address,
	})
	if err != nil {
		response.SrvError(c, "查询地址失败", err.Error())
		return
	}

	// 记录地址查询经纬度行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "地址查询经纬度", map[string]interface{}{
		"address": req.Address,
		"success": err == nil,
	})

	response.Success(c, "获取成功", loc)
}
